package secondriver.aboxlibrary.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.StrictMode;
import android.support.annotation.IdRes;

/**
 * Author : secondriver
 * Created : 2015/12/8
 */
public class AppUtil {
    /**
     * 判断App是否是Debug模式
     *
     * @param context
     * @return
     */
    public static boolean isDebugModeApp(Context context) {
        if (null == context) {
            throw new NullPointerException("Context must not be null.");
        }
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo info = pm.getApplicationInfo(context.getPackageName(), 0);
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return true;
    }

    /**
     * 设置当前线程的StrictMode 检查所有类型的网络和IO
     */
    public static void setStrictModeOnCurrentThread() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll().penaltyLog().penaltyDialog().build());
    }

    /**
     * 设置所有线程的StrictMode 检查所有类型的网络和IO
     */
    public static void setStrictModeOnAllThread() {
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll().penaltyLog().penaltyDeath().build());
    }

    public static Uri getURIFromResourcesID(Context context, @IdRes int resId) {
        Resources resources = context.getResources();
        Uri u = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + resources.getResourcePackageName(resId) + '/'
                + resources.getResourceTypeName(resId) + '/'
                + resources.getResourceEntryName(resId));
        return u;
    }

}
